package com.gxa.ehome.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.ehome.dao.GetVoDao;
import com.gxa.ehome.dao.impl.GetVoDaoImpl;
import com.gxa.ehome.entity.Cp;
import com.gxa.ehome.entity.vo.GetVo;
import com.gxa.ehome.service.CpService;
import com.gxa.ehome.service.GetVoService;
import com.gxa.ehome.service.impl.CpServiceImpl;
import com.gxa.ehome.service.impl.GetVoServiceImpl;
import com.gxa.ehome.util.JsonResult;
import com.gxa.ehome.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GetVoServlet", urlPatterns = "/getVo")
public class GetVoServlet extends HttpServlet {
    private GetVoService getVoService = new GetVoServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理请求中的中文, 使用UTF-8编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");   // 服务器端的字符集设置(影响流)- java代码相关
        response.setHeader("Content-Type", "text/html;charset=UTF-8"); // 浏览器行为

        JsonResult jsonResult = new JsonResult();
        String method = request.getHeader("method");
//        System.out.println(method);
        switch (method) {
            case "getCpnList": {
                String page = request.getParameter("page");
                String limit = request.getParameter("limit");
//                System.out.println(page);
//                System.out.println(limit);
                try {
                    List<GetVo> locps = getVoService.selectAllByPage(Integer.parseInt(page), Integer.parseInt(limit));
                    jsonResult.setCode("0");
                    jsonResult.setData(locps);
                    jsonResult.setValue("count",locps.size());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "search": {
                String page = request.getParameter("page");
                String limit = request.getParameter("limit");
                String searchString=request.getParameter("searchString");
                try {
                    List<GetVo> searchlist=getVoService.search(searchString,Integer.parseInt(page), Integer.parseInt(limit));
                    jsonResult.setCode("0");
                    jsonResult.setData(searchlist);
                    jsonResult.setValue("count",searchlist.size());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            }
            case "getCpn": {
                Integer cpId = Integer.parseInt(request.getParameter("cpId"));
                String phone = request.getParameter("phone");
                try {
                    if (getVoService.getCpn(cpId,phone)){
                        jsonResult.setCode("1");
                        jsonResult.setMsg("领取成功");
                    }else{
                        jsonResult.setCode("0");
                        jsonResult.setMsg("领取失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "getCpnListForWeb": {
                String phone = request.getParameter("phone");
                GetVoDao getVoDao=new GetVoDaoImpl();
                HashMap<String, Object> stringListHashMap = new HashMap<>();
                try {
                    List<Cp> userNotGetCpnList = getVoDao.selectNotGetCpsByPhone(phone);
                    List<Cp> userGetCpnList = getVoDao.selectGetCpsByPhone(phone);
                    List<Cp> overExpire =getVoDao.selectOverExpireCpsByPhone(phone);
                    stringListHashMap.put("userNotGetCpnList",userNotGetCpnList);
                    stringListHashMap.put("userGetCpnList",userGetCpnList);
                    stringListHashMap.put("overExpire",overExpire);
                    jsonResult.setCode("1");
                    jsonResult.setMsg("查询成功");
                    jsonResult.setData(stringListHashMap);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            default: {
                break;
            }
        }
        ResponseUtil.send(response, JSON.toJSONString(jsonResult.getValues()));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
